package com.tms.app.tms.ui.base.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jph.takephoto.model.TResult;
import com.tms.app.tms.bean.ImgBean;
import com.tms.app.tms.ui.ShowPhotoActivity;
import com.tms.app.tms.R;
import com.tms.app.tms.utils.PhotoHelper;
import com.tms.app.tms.utils.ToolsUtil;
import com.tms.app.tms.widget.MyDialogPhoto;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.zafu.camera.activity.CameraIdCradActivity;

/**
 * Created by Administrator on 2017/11/2 0002.
 */

public abstract class AbsPhotoPickActivity extends BasePhotoActivity {
    public ArrayList<View> viewList = new ArrayList<>();
    public Map<View,String> imgSelectList = new HashMap<>();//本地图片集合
    public Map<View,String> imgServerList=new HashMap<>();//远程图集合
    public ArrayList<ImageView> imgViewList = new ArrayList<>();//本地所有 图片容器
    public ArrayList<ImageView> showImgList = new ArrayList<>();// 显示的图片容器
    public ArrayList<TextView> imgDelList = new ArrayList<>();

    public MyDialogPhoto myDialogPhoto;
    private ImageView indexImgView;
    private String IMAGE_FILE_NAME="img1.jpg";
    private String CROP_IMAGE_FILE_NAME="img2.jpg";
    private String IMAGE_GALLERY_NAME="img3.jpg";
    private File mCameraFile;
    private File mCropFile;
    private File mGalleryFile;
    private int CAMERA_REQUEST_CODE=0x1024;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       initLayout();
        initData();
        String baseFilePath= ToolsUtil.getApkDir();
        mCameraFile = new File(baseFilePath, IMAGE_FILE_NAME);//照相机的File对象
        mCropFile = new File(baseFilePath, CROP_IMAGE_FILE_NAME);//裁剪后的File对象
        mGalleryFile = new File(baseFilePath, IMAGE_GALLERY_NAME);//相册的File对象

    }
    public abstract void initLayout();

    protected void initData() {
        super.onStart();
        imgViewList.clear();
        imgDelList.clear();
        imgSelectList.clear();
        viewList.clear();
        imgSelectList.clear();
        imgServerList.clear();
        showImgList.clear();

        View view1=findViewById(R.id.img_parent_1);
        View view2=findViewById(R.id.img_parent_2);
        View view3=findViewById(R.id.img_parent_3);
        View view4=findViewById(R.id.img_parent_4);

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);

        imgDelList.add((TextView) findViewById(R.id.photo_button_del_1));
        imgDelList.add((TextView) findViewById(R.id.photo_button_del_2));
        imgDelList.add((TextView) findViewById(R.id.photo_button_del_3));
        imgDelList.add((TextView) findViewById(R.id.photo_button_del_4));

        ImageView imageView1= (ImageView) findViewById(R.id.img_1);
        ImageView imageView2= (ImageView) findViewById(R.id.img_2);
        ImageView imageView3= (ImageView) findViewById(R.id.img_3);
        ImageView imageView4= (ImageView) findViewById(R.id.img_4);

        imageView1.setTag(R.id.img_tag,view1);
        imageView2.setTag(R.id.img_tag,view2);
        imageView3.setTag(R.id.img_tag,view3);
        imageView4.setTag(R.id.img_tag,view4);

        imgViewList.add(imageView1);
        imgViewList.add(imageView2);
        imgViewList.add(imageView3);
        imgViewList.add(imageView4);

        for (ImageView imageView : imgViewList) {
            imgSelectList.put(imageView,"-1");
            imgServerList.put(imageView,"-1");
            imageView.setOnClickListener(this);
           // imageView.setOnLongClickListener(this);
        }
        for (TextView textView : imgDelList) {
            textView.setOnClickListener(this);
        }

        findViewById(R.id.line_photo_take_pick).setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if ((System.currentTimeMillis() - mExitTime) < 2000) {
            mExitTime = System.currentTimeMillis();
            return;
        }
        Intent intent;

       switch (v.getId()){
           case  R.id.img_1:
           case  R.id.img_2:
           case  R.id.img_3:
           case  R.id.img_4:
               intent=new Intent(this,ShowPhotoActivity.class);
               if (imgSelectList.get(v)==null||imgSelectList.get(v).equals("-1")){
                   intent.putExtra("imgPath", imgServerList.get(v));
                   intent.putExtra("imgType",0);
               }else {
                   intent.putExtra("imgPath", imgSelectList.get(v));
                   intent.putExtra("imgType",0);
               }
               startActivity(intent);
               indexImgView= (ImageView) v;
               break;
           case R.id.line_photo_take_pick:
               myDialogPhoto = new MyDialogPhoto(this, this, 1);
               myDialogPhoto.show();
               break;
           case R.id.dialog_photo_text_top_1:
              // PhotoHelper.takePhoto(getTakePhoto(),false);
//               Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//               if (ToolsUtil.hasSDCard) {
//                   if (Build.VERSION.SDK_INT >= 24) {//7.0及以上
//                   Uri uriForFile = FileProvider.getUriForFile(this, "com.tms.app.tms.fileprovider", mCameraFile);
//                   intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
//                   intentFromCapture.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//                   intentFromCapture.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); }
//               else {
//                   intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCameraFile));
//                    }
//               }
//               startActivityForResult(intentFromCapture, CAMERA_REQUEST_CODE);

               intent = new Intent(this, CameraIdCradActivity.class);
               intent.putExtra("path", "/sdcard/tms/img/");
               intent.putExtra("name", System.currentTimeMillis()+".jpg");
               intent.putExtra("type", "idcardFront");
               startActivityForResult(intent,101);

               if (myDialogSelect != null)
                   myDialogSelect.close();
               if (myDialogPhoto != null)
                   myDialogPhoto.close();
               indexImgView=imgViewList.get(showImgList.size());
               break;
           case R.id.dialog_photo_text_top_2:
               PhotoHelper.pickPhoto(getTakePhoto(),false);
               if (myDialogSelect != null)
                   myDialogSelect.close();
               if (myDialogPhoto != null)
                   myDialogPhoto.close();
               indexImgView=imgViewList.get(showImgList.size());
               break;
           default:
               doDelImg(v);
       }
    }


    private void doDelImg(View v){
        for (int i = 0; i < imgDelList.size(); i++) {
            if (v==imgDelList.get(i)){
                if (!imgServerList.get(imgViewList.get(i)).equals("-1")){
                    delSerImg(i);
                }
                imgSelectList.put(imgViewList.get(i),"-1");
                imgServerList.put(imgViewList.get(i),"-1");
                showImgList.remove(imgViewList.get(i));
            }
        }
        loadPickPhoto();
    }

    public abstract void delSerImg(int index);


    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        if (result.getImage()!=null) {
            for (ImageView imageView : imgViewList) {
                if (!showImgList.contains(imageView)){
                    indexImgView=imageView;
                    break;
                }
            }
            imgSelectList.put(indexImgView,result.getImage().getCompressPath());
        }
        loadPickPhoto();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101) {
            if (resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                String path = extras.getString("path");
                if (path!=null){
                    for (ImageView imageView : imgViewList) {
                        if (!showImgList.contains(imageView)){
                            indexImgView=imageView;
                            break;
                        }
                    }
                    imgSelectList.put(indexImgView,path);
                }
                loadPickPhoto();
            }
        }
    }

    public void loadPickPhoto() {
        for (View view : viewList) {
            view.setVisibility(View.GONE);
        }
        for (View view : imgServerList.keySet()) {
            if (imgServerList.get(view)!=null&&!imgServerList.get(view).equals("-1")){
                view.setVisibility(View.VISIBLE);
                ImageView imageView= (ImageView) view;
                String imgPath=imgServerList.get(view);
                Glide.with(this).load(imgPath).into(imageView);
                if (!showImgList.contains(imageView))
                    showImgList.add(imageView);
            }
        }
        for (View view : imgSelectList.keySet()) {
            if (imgSelectList.get(view)!=null&&!imgSelectList.get(view).equals("-1")){
                view.setVisibility(View.VISIBLE);
                ImageView imageView= (ImageView) view;
                String imgPath=imgSelectList.get(view);
                Glide.with(this).load(new File(imgPath)).into(imageView);
                if (!showImgList.contains(imageView))
                    showImgList.add(imageView);
            }
        }

        for (ImageView imageView : showImgList) {
           View view= (View) imageView.getTag(R.id.img_tag);
            view.setVisibility(View.VISIBLE);
        }

        findViewById(R.id.line_photo_take_pick).setVisibility(View.VISIBLE);
        if (showImgList.size() >= 4) {
            findViewById(R.id.line_photo_take_pick).setVisibility(View.GONE);
        }
        ((TextView)findViewById(R.id.text_photo_number)).setText(showImgList.size()+"/4");
    }

}